Refactor (packages/ui/src/theme/v2/mapping.ts:34): Found 64 lines of similar code in 2 locations - #66
Open
relghali wants to merge 4 commits into
Open
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Issue for this PR
Closes #60
Type of change
What does this PR do?
Initially, Qlty had flagged 64 lines to have similar code in two locations in the 'packages/ui/src/theme/v2/mapping.ts' file. The 'light' and 'dark' token tables had the same exact keys in the same order and only differed in their values. This meant every new token had to be added twice, which is what caused duplicated code.
To fix this, I merged each pair of tokens into a single table of ['light', 'dark'] tuples, then used the helper function 'pick' to construct the 'light' and 'dark' with less repetition by indexing sets of tokens with 0 for 'light' and 1 for 'dark'. This works because the token order is unchanged and every value is the same, it is just the tables are reorganized so that each token is written once.
This PR also includes my .devcontainer changes, since the bun image doesn't come with curl, git, or xz-utils (which Qlty installer needs)
How did you verify your code works?
There was actually no tests for this file at all and it didn't show up in the coverage report, so I added 'packages/ui/src/theme/v2/mapping.test.ts' to test the changes. It checks the exact values (from the original file) for both themes, covers each kind of value in the file (e.g. var() refs, hex, rgba(), etc.), and that 'light' and 'dark' contain the same tokens just as the original did.
To make sure I did not accidentally mistype/miscopy a value while fixing the code, I dumped the full output of mapV2Semantics(false) and mapV2Semantics(true) to JSON before and after the change and diffed them, and found the diff to be empty.
I also made sure these tests worked by writing incorrect code (e.g. swapping a 'dark' value with a 'light' value) and having the tests prove that there is a failure.
bun lint and bun test pass locally. The CI config already runs packages/ui's tests and the test script picks up everything under src, so the new test file runs on this branch without any CI changes.
Screenshots / recordings
Note that while I changed a file in the UI, there was no UI changed as all the token values remained the exact same, I simply re-structured the code to do the same thing.
Evidences:
Before:

After:

Checklist